package com.loukou.lkt.web.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.loukou.lkt.lkt.redis.RedisService;
import com.loukou.lkt.lkt.resp.AuthInfo;

public class BaseController {
	
	@Autowired
	protected HttpSession session;
	
	@Autowired
	private RedisService redisService;

	protected String getSessionId(){
		return session.getId();
	}
	
	@ModelAttribute
	public void autoRun(Model model,HttpServletRequest request) {
        List<String> privileges = new ArrayList<String>();

		AuthInfo info = redisService.getAuthInfo(getSessionId());
		if (info!=null && !CollectionUtils.isEmpty(info.getPrivileges())) {
			privileges.addAll(info.getPrivileges());
		}
		model.addAttribute("privileges", privileges);
    }
	
	public AuthInfo getAuthInfo(){

		AuthInfo info = redisService.getAuthInfo(getSessionId());
		return info;
	}
	
	protected List<Integer> getWarehosueIds(){
		return  Arrays.asList(1,2,3,4,5);
	}
}
